import { reqLogin, reqUserId } from '@/service/user';
import { message } from 'antd';
import router from 'umi/router';

const Model = {
    namespace: 'user',
    state: {
        role: '',
        info: '',
    },
    effects: {
        *reqLogin({ payload }, { call, put }) {
            console.log('mod', payload)
            const res = yield call(reqLogin, payload)
            if (res.code === 200) {
                console.log(res.token)
                localStorage.setItem('token', res.token)
                localStorage.setItem('shale_sys_role', res.role)
                yield put({
                    type: 'reqUserId',
                    payload: payload.username
                })
            }
        },
        *reqUserId({ payload }, { put, call }) {
            const res = yield call(reqUserId, payload);
            localStorage.setItem('realName', res?.User.realname)
            if (res.code === 200) {
                console.log(res);
                localStorage.setItem('shale_sys_userid', res.User.id)
                router.push('/work/sample');
                yield put({
                    type: 'saveRealName',
                    payload: res?.User.realname
                })
            }
        },
    },
    reducers: {
        saveRole(state, { payload }) {
            return { ...state, role: payload }
        },
        saveRealName(state, { payload }) {
            return { ...state, realname: payload }
        }
    }
}
export default Model